package blow.sky.quoted.service;

import blow.sky.quoted.dao.ResponseDao;
import blow.sky.quoted.dao.UserInfoDao;
import blow.sky.quoted.entity.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName QuoteResponseService
 * @Description TODO
 * @Author 江志武
 * @Date 2021/6/1 20:32
 */
@Service
public class QuoteResponseService {
    @Autowired
    ResponseDao responseDao;
    @Autowired
    UserInfoDao infoDao;
    //添加表信息
    public boolean insertOne(UserViewResponse demandResponse)
    {
        List<List<String>> json = demandResponse.getQuote();
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = null;
        try {
            jsonStr = mapper.writeValueAsString(json);
        }
        catch (JsonProcessingException e) {
            e.printStackTrace();
            return false;
        }
        QuoteResponse quoteResponse = new QuoteResponse();
        BeanUtils.copyProperties(demandResponse, quoteResponse);
        quoteResponse.setQuotes(jsonStr);
        responseDao.save(quoteResponse);
        return true;
    }
    //通过requestId查找
    public List<UserViewResponse> findByRequestId(long id)
    {
        List<QuoteResponse> responses = responseDao.findAllByToRequestId(id);
        List<UserViewResponse> viewResponses = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        for (QuoteResponse response : responses)
        {
            String jsonStr = response.getQuotes();
            try {
                List<List<String>> json = mapper.readValue(jsonStr, List.class);
                UserViewResponse viewResponse = new UserViewResponse();
                BeanUtils.copyProperties(response, viewResponse);
                viewResponse.setQuote(json);
                viewResponses.add(viewResponse);
            }
            catch (JsonMappingException e) {
                e.printStackTrace();
            }
            catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return viewResponses;
    }
    //通过用户查找
    public List<UserViewResponse> findByUser(String user)
    {
        List<QuoteResponse> responses = responseDao.findAllByFromUser(user);
        List<UserViewResponse> viewResponses = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        for (QuoteResponse response : responses)
        {
            String jsonStr = response.getQuotes();
            try {
                List<List<String>> json = mapper.readValue(jsonStr, List.class);
                UserViewResponse viewResponse = new UserViewResponse();
                BeanUtils.copyProperties(response, viewResponse);
                viewResponse.setQuote(json);
                viewResponses.add(viewResponse);
            }
            catch (JsonMappingException e) {
                e.printStackTrace();
            }
            catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return viewResponses;
    }


    //获取信息并附加info
    public List<ClientViewResponse> findResponseAndInfo(List<UserViewResponse> userViewResponses)
    {
        try {
            List<ClientViewResponse> clientViewResponses = new ArrayList<>();
            for (UserViewResponse userViewResponse : userViewResponses) {
                UserInfo info = new UserInfo();
                ClientViewResponse clientViewResponse = new ClientViewResponse();
                try{
                    info = infoDao.findByEmail(userViewResponse.getFromUser());
                    BeanUtils.copyProperties(info, clientViewResponse);
                    BeanUtils.copyProperties(userViewResponse, clientViewResponse);
                    clientViewResponses.add(clientViewResponse);
                }
                catch (Exception e)
                {
                    e.printStackTrace();

                }

            }
            return clientViewResponses;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
        }

}
